package com.znyj.cloud.device.async;

import com.znyj.cloud.chain.util.SpringContextHelper;
import com.znyj.cloud.device.constant.SysConstant;
import com.znyj.cloud.device.entity.TaskEntity;
import com.znyj.cloud.device.service.ReportService;
import com.znyj.cloud.device.service.TaskService;
import org.apache.log4j.Logger;

import java.sql.Timestamp;

/**
 * Created by gaoliwei02 on 17/6/5.
 */
//异步操作任务包数据库
public class TaskAsync implements Runnable {
    private static final Logger logger = Logger.getLogger(TaskAsync.class);
    private TaskEntity taskEntity;
    private int op;
    private int sendStatus;
    private long orderId;

    //构造
    public TaskAsync(TaskEntity taskEntity, int op) {
        this.taskEntity = taskEntity;
        this.op = op;
    }

    //构造
    public TaskAsync(long orderId, int sendStatus, int op) {
        this.sendStatus = sendStatus;
        this.orderId = orderId;
        this.op = op;
    }

    @Override
    public void run() {
        TaskService taskervice = SpringContextHelper.getBean(TaskService.class);
        ReportService reportService = SpringContextHelper.getBean(ReportService.class);
        switch (op) {
            case SysConstant.CDB_OP.INSERT_TASK: {
                taskervice.insertTask(taskEntity);
            }
            break;

            case SysConstant.CDB_OP.UPDATE_SEND_STATUS: {
                //更新reportDB，确定发送成功
                if (sendStatus == 1) {
                    TaskEntity taskEntity = taskervice.findByOrderId(orderId);
                    switch (taskEntity.getControlMsg()) {
                        case "cdbaopen": {
                            taskervice.findByOrderIdAndUpdateStatus(orderId, sendStatus);
                            reportService.updateAInfo(
                                    taskEntity.getDeviceId(),
                                    taskEntity.getCreateTime(),
                                    taskEntity.getDurationSec(),
                                    taskEntity.getUserId(),
                                    taskEntity.getOrderId());
                        }
                        break;

                        case "cdbbopen": {
                            taskervice.findByOrderIdAndUpdateStatus(orderId, sendStatus);
                            reportService.updateBInfo(
                                    taskEntity.getDeviceId(),
                                    taskEntity.getCreateTime(),
                                    taskEntity.getDurationSec(),
                                    taskEntity.getUserId(),
                                    taskEntity.getOrderId());
                        }
                        break;

                        case "cdbaclose": {
                            long lastAOpen = reportService.getChannalAOrderId(taskEntity.getDeviceId());
                            taskervice.findByOrderIdAndUpdateStatusWithExecute("close last A:" + String.valueOf(lastAOpen), orderId, sendStatus);
                            reportService.updateAInfo(taskEntity.getDeviceId(), new Timestamp(SysConstant.DEFAULT_TIME), 0, "", 0);
                        }
                        break;

                        case "cdbbclose": {
                            long lastBOpen = reportService.getChannalBOrderId(taskEntity.getDeviceId());
                            taskervice.findByOrderIdAndUpdateStatusWithExecute("close last B:" + String.valueOf(lastBOpen), orderId, sendStatus);
                            reportService.updateBInfo(taskEntity.getDeviceId(), new Timestamp(SysConstant.DEFAULT_TIME), 0, "", 0);
                        }
                        break;
//                    case "cdbopen": {
//                        cdbTaskervice.findByOrderIdAndUpdateStatus("", orderId, sendStatus);
//                        cdbReportService.updateABInfo(
//                                cdbTaskEntity.getDeviceId(),
//                                cdbTaskEntity.getCreateTime(),
//                                cdbTaskEntity.getDurationSec(),
//                                cdbTaskEntity.getUserId(),
//                                cdbTaskEntity.getOrderId(),
//                                cdbTaskEntity.getCreateTime(),
//                                cdbTaskEntity.getDurationSec(),
//                                cdbTaskEntity.getUserId(),
//                                cdbTaskEntity.getOrderId(),
//                                1, 1
//                        );
////                        cdbReportService.updateAInfo(
////                                cdbTaskEntity.getDeviceId(),
////                                cdbTaskEntity.getCreateTime(),
////                                cdbTaskEntity.getDurationSec(),
////                                cdbTaskEntity.getUserId(),
////                                cdbTaskEntity.getOrderId());
////                        cdbReportService.updateBInfo(
////                                cdbTaskEntity.getDeviceId(),
////                                cdbTaskEntity.getCreateTime(),
////                                cdbTaskEntity.getDurationSec(),
////                                cdbTaskEntity.getUserId(),
////                                cdbTaskEntity.getOrderId());
//                    }
//                    break;
//
//                    case "cdbclose": {
//                        long lastAOpen = cdbReportService.getChannalAOrderId(cdbTaskEntity.getDeviceId());
//                        cdbTaskervice.findByOrderIdAndUpdateStatus(String.valueOf(lastAOpen), orderId, sendStatus);
////                        cdbReportService.updateAInfo(cdbTaskEntity.getDeviceId(), new Timestamp(SysConstant.DEFAULT_TIME), 0, "", 0);
////                        cdbReportService.updateBInfo(cdbTaskEntity.getDeviceId(), new Timestamp(SysConstant.DEFAULT_TIME), 0, "", 0);
//                        cdbReportService.updateABInfo(
//                                cdbTaskEntity.getDeviceId(),
//                                new Timestamp(SysConstant.DEFAULT_TIME), 0, "", 0,
//                                new Timestamp(SysConstant.DEFAULT_TIME), 0, "", 0,
//                                0, 0
//                        );
//                    }
//                    break;
                        default:
                            break;
                    }
                }
            }
            break;
        }
    }
}